package com.xiaotu.spider.baidu.pageprocessor;

import java.util.List;
import java.util.Map;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xiaotu.common.model.DieStarModel;
import com.xiaotu.common.util.BaseMsgUtils;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.server.service.guduo.SupplementGuDuoRecordService;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

/**
 * 
 * 项目名称：Sprider-WebMagic    
 * 类名称：StarBaiduBaikePageProcessor    
 * 类描述：艺人百度百科信息
 * 创建人：张晓   
 * 创建时间：2020年5月25日 上午11:57:18    
 * @version
 */
@Component("StarBaiduBaikePageProcessor")
public class StarBaiduBaikePageProcessor extends AUrlPageProcessor
{
    private String start_url = "https://www.baidu.com/";
	
    @Autowired
	private SupplementGuDuoRecordService starService;
    
    @Override
	protected String[] getStartRequestUrls() {
    	return new String[]{start_url};
	}
	
    @Override
   	public void process(Page page) {
    	if (Constants.PageDataType.STAR_RECORD.equals(this.getPageDataType(page)))
            this.tvRecord(page);
        else
            this.pageProcess(page);
    }
    
    private void pageProcess(Page page) {
    	//StringBuffer str = new StringBuffer();
    	List<Map<String,Object>> starList = starService.getStarInfoList();
    	for (Map<String, Object> starInfoMap : starList) {
    		String linkUrl = "https://baike.baidu.com/item/"+starInfoMap.get("name")+"?starid="+starInfoMap.get("starid")+"&occupation="+starInfoMap.get("occupation");
    		PageUtils.addTargetRequest(page, linkUrl, page.getRequest().getExtras());
    		//str.append("http://baike.baidu.com/item/"+KeywordEncode.encode(starInfoMap.get("name")+"")+"?starid="+starInfoMap.get("starid")+"&occupation="+KeywordEncode.encode(starInfoMap.get("occupation")+"")+"").append(",");
    	}
    }
    
	private void tvRecord(Page page) {
    	String linkUrl = page.getUrl().toString();
    	String resOccupation=null;
    	String starName = null;
    	String starId = null;
    	if(linkUrl.contains("occupation")){
    		resOccupation = linkUrl.substring(linkUrl.indexOf("occupation=")+11, linkUrl.length());	
    	}
    	starName = linkUrl.substring(linkUrl.indexOf("item/")+5, linkUrl.indexOf("?starid"));
    	starId = linkUrl.substring(linkUrl.indexOf("starid=")+7, linkUrl.indexOf("&occupation"));
		List<String> nameList = page.getHtml().xpath("//dt[@class='basicInfo-item name']").all();
		List<String> valueList = page.getHtml().xpath("//dd[@class='basicInfo-item value']").all();
		String name = null, occupation=null, diedate=null;
		boolean occflag = false;
		if (nameList != null && !nameList.isEmpty() && nameList != null && nameList.size() ==
				valueList.size()){
			for (int j = 0; j < nameList.size(); j++) {
				if(nameList.get(j).contains("中文名")){
					name = valueList.get(j);
				}
				if(nameList.get(j).contains("职") && nameList.get(j).contains("业")){
					occupation = valueList.get(j);
				}
				if(nameList.get(j).contains("逝世日期")){
					diedate = valueList.get(j);
				}
			}
			if(StringUtil.isEmpty(diedate))
				return;
			//String resOccupation = starInfoMap.get("occupation")+"";
			if(StringUtil.isNotEmpty(resOccupation)){
				String[] occupationArr = resOccupation.split(",");
				for (String occupationStr : occupationArr) {
					if(StringUtil.isNotEmpty(occupation)){
						if(occupation.contains(occupationStr)){
							occflag = true;
							break;
						}
					}
				}
			}
			Html dieHtml = new Html(diedate);
			diedate = dieHtml.xpath("//dd/text()").get().trim();
			if(StringUtil.isNotEmpty(diedate)){
				diedate = BaseMsgUtils.birthString(diedate.trim());
			}
			
			if(StringUtil.isNotEmpty(name) && StringUtil.isNotEmpty(starName)){
				if(name.contains(starName) && occflag){
					page.putField(DieStarModel.class.getName(),new DieStarModel(starId, starName, diedate));
				}
			}
			
		}
    }
    
	/*@Override
	public void process(Page page) {
	List<Map<String,Object>> starList = starService.getStarInfoList();
	Set<DieStarModel> starSet = new HashSet<>();
	for (Map<String, Object> starInfoMap : starList) {
	try {
		Connection conn = connent("https://baike.baidu.com/item/"+KeywordEncode.encode(starInfoMap.get("name")+""));
		conn.get();
		Response rs = conn.response();
		String html = rs.body();
		if(StringUtil.isNotEmpty(html)){
			Html htmlDiv = new Html(html);
			List<String> nameList = htmlDiv.xpath("//dt[@class='basicInfo-item name']").all();
			List<String> valueList = htmlDiv.xpath("//dd[@class='basicInfo-item value']").all();
			String name = null, occupation=null, diedate=null;
			boolean occflag = false;
			if (nameList != null && !nameList.isEmpty() && nameList != null && nameList.size() ==
					valueList.size()){
				for (int j = 0; j < nameList.size(); j++) {
					if(nameList.get(j).contains("中文名")){
						name = valueList.get(j);
					}
					if(nameList.get(j).contains("职") && nameList.get(j).contains("业")){
						occupation = valueList.get(j);
					}
					if(nameList.get(j).contains("逝世日期")){
						diedate = valueList.get(j);
					}
				}
				if(StringUtil.isEmpty(diedate)) 
					continue;
				String resOccupation = starInfoMap.get("occupation")+"";
				if(StringUtil.isNotEmpty(resOccupation)){
					String[] occupationArr = resOccupation.split(",");
					for (String occupationStr : occupationArr) {
						if(StringUtil.isNotEmpty(occupation)){
							if(occupation.contains(occupationStr))
								occflag = true;
								break;
						}
					}
				}
				Html dieHtml = new Html(diedate);
				diedate = dieHtml.xpath("//dd/text()").get().trim();
				if(StringUtil.isNotEmpty(diedate)){
					diedate = BaseMsgUtils.birthString(diedate.trim());
				}
				
				if(StringUtil.isNotEmpty(name) && StringUtil.isNotEmpty(starInfoMap.get("name")+"")){
					if(name.contains(starInfoMap.get("name")+"") && occflag){
						starSet.add(new DieStarModel(starInfoMap.get("starid")+"", starInfoMap.get("name")+"", diedate));
					}
				}
				
			}
		}
		
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
			if (!starSet.isEmpty()){
				page.putField(DieStarModel.class.getName(), starSet);
			}
}*/

	@Override
	protected String getSpiderType() {
		return Constants.SpiderTaskType.Star_BAIDU;
	}

	@Override
	public String getPageDataType(Page page) {
		String url = page.getUrl().toString();
        if (url.contains("item"))
        	return Constants.PageDataType.STAR_RECORD;
		return Constants.PageDataType.SEARCH_LIST;
	}

	//链接目标url
  	public static Connection connent(String url) {
      	Connection conn = Jsoup.connect(url)    
	      .userAgent("Mozilla/5.0 (Windows NT 6.1; rv:47.0) Gecko/20100101 Firefox/47.0") // http请求的浏览器设置
	      .timeout(5000)   // http连接时长
	      .maxBodySize(0)
	      .method(Connection.Method.GET);
	      //.cookie("cookie", PropertiesUtil.getProperty("DouBanTVNotBrd.site.Cookie"));  	
      	return conn;
  	}
}
